package com.lanqiao.controller;

import cn.hutool.json.JSONUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.lanqiao.mvc.entity.MultipartFile;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/upload")
public class UploadPictureController {

    /**
     * 图片上传方式一
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/uploadPicture.do")
    @YockMvcAnnotation.ResponseBody
    public String uploadPicture(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存图片名称的集合
        Map<String,String> map = new HashMap<>();
        // 获取上传文件对象
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        try {
            // 解析请求的内容提取文件数据
            List<FileItem> items = upload.parseRequest(request);
            // 迭代表单数据
            for (FileItem item : items) {
                // 大图
               if("book_logo_big".equals(item.getFieldName())){
                   //设置上传图片路径（访问路径）
                   String uploadPath = request.getServletContext().getRealPath("upload");
                   //获取图片上传名称（保证图片的唯一性）
                   String fileName = System.currentTimeMillis() + item.getName();
                   // 把图片加载到路径中（保证图片的唯一性）
                   uploadPath += File.separator + fileName;
                   // 保存文件到硬盘
                   item.write(new File(uploadPath));
                   // 保存图片名称
                   map.put("book_logo_big",fileName);
               }

               // 小图
                if("book_logo_small".equals(item.getFieldName())){
                    //设置上传图片路径
                    String uploadPath = request.getServletContext().getRealPath("upload");
                    //获取图片上传名称（保证图片的唯一性）
                    String fileName = System.currentTimeMillis() + item.getName();
                    // 把图片加载到路径中（保证图片的唯一性）
                    uploadPath += File.separator + fileName;
                    // 保存文件到硬盘
                    item.write(new File(uploadPath));
                    // 保存图片名称
                    map.put("book_logo_small",fileName);
                }
                //循环上传其它图片
                for(int i = 1; i <= 5; i++){
                    String pic_name = "pic_name" + i;
                    if(pic_name.equals(item.getFieldName())){
                        //设置上传图片路径
                        String uploadPath = request.getServletContext().getRealPath("upload");
                        //获取图片上传名称
                        String fileName = System.currentTimeMillis() + item.getName();
                        // 把图片加载到路径中
                        uploadPath += File.separator + fileName;
                        // 保存文件到硬盘
                        item.write(new File(uploadPath));
                        // 保存图片名称
                        map.put(pic_name,fileName);
                    }
                }

            }

        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回图片名称集
        String jsonStr = JSONUtil.toJsonStr(map);
        return jsonStr;
    }


    /**
     * 图片上传方式二
     * @param request
     * @param response
     * @param book_logo_big
     * @param book_logo_small
     * @param pic_name1
     * @param pic_name2
     * @param pic_name3
     * @param pic_name4
     * @param pic_name5
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/uploadPicture2.do")
    @YockMvcAnnotation.UploadFile
    @YockMvcAnnotation.ResponseBody
    public String uploadPicture2(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "book_logo_big") MultipartFile book_logo_big,
            @YockMvcAnnotation.RequestParam(name = "book_logo_small") MultipartFile book_logo_small,
            @YockMvcAnnotation.RequestParam(name = "pic_name1") MultipartFile pic_name1,
            @YockMvcAnnotation.RequestParam(name = "pic_name2") MultipartFile pic_name2,
            @YockMvcAnnotation.RequestParam(name = "pic_name3") MultipartFile pic_name3,
            @YockMvcAnnotation.RequestParam(name = "pic_name4") MultipartFile pic_name4,
            @YockMvcAnnotation.RequestParam(name = "pic_name5") MultipartFile pic_name5
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存图片名称的集合
        Map<String,String> map = new HashMap<>();
        String uploadPath = request.getServletContext().getRealPath("upload");

        if(book_logo_big.getName() != null){
            String fileName = System.currentTimeMillis() +  book_logo_big.getName();
            // 上传图片
            book_logo_big.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("book_logo_big",fileName);
        }

        if(book_logo_small.getName() != null){
            String fileName = System.currentTimeMillis() +  book_logo_small.getName();
            book_logo_small.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("book_logo_small",fileName);
        }

        if(pic_name1.getName() != null){
            String fileName = System.currentTimeMillis() +  pic_name1.getName();
            pic_name1.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("pic_name1",fileName);
        }

        if(pic_name2.getName() != null){
            String fileName = System.currentTimeMillis() +  pic_name2.getName();
            pic_name2.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("pic_name2",fileName);
        }

        if(pic_name3.getName() != null){
            String fileName = System.currentTimeMillis() +  pic_name3.getName();
            pic_name3.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("pic_name3",fileName);
        }

        if(pic_name4.getName() != null){
            String fileName = System.currentTimeMillis() +  pic_name4.getName();
            pic_name4.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("pic_name4",fileName);
        }

        if(pic_name5.getName() != null){
            String fileName = System.currentTimeMillis() +  pic_name5.getName();
            pic_name5.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("pic_name5",fileName);
        }

        //返回图片名称集
        String jsonStr = JSONUtil.toJsonStr(map);
        return jsonStr;
    }






    /**
     * 轮播图片上传图片
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/shufflingupload.do")
    @YockMvcAnnotation.UploadFile
    @YockMvcAnnotation.ResponseBody
    public String shufflingupload(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "shuffling_pic") MultipartFile shuffling_pic
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存图片名称的集合
        Map<String, Object> map = new HashMap<>();

        //设置上传图片路径（访问路径）
        String uploadPath = request.getServletContext().getRealPath("upload");


        if (shuffling_pic.getName()!=null){
            //获取图片上传名称（保证图片的唯一性）
            String fileName = System.currentTimeMillis() +  shuffling_pic.getName();
            // 上传图片
            shuffling_pic.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("shuffling_pic",fileName);
        }



        //返回图片名称集
        String jsonStr = JSONUtil.toJsonStr(map);
        System.out.println(jsonStr);
        return jsonStr;
    }
}
